<template>
  <div>
    <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        class="loginContainer"
        v-loading="loading"
    >
      <h3 class="loginTitle">系统登录</h3>
      <!--表单项绑定了数据才能输入-->
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input type="text" v-model="loginForm.code" style="width: 250px;margn-right: 5px;"></el-input>
        <img :src="captchaUrl" @click="loginCaptcha" style="height: 40px">
      </el-form-item>
      <el-checkbox v-model="checked" class="loginRemeber">记住我</el-checkbox>
      <el-button type="primary" style="width: 100%;" @click="submitLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: '123',
          code: ''
        },
        captchaUrl: `/captcha?time=${Date.now()}`, // 每次不同以免响应重复验证图片
        checked: true,
        loading:false, // 加载动画
        rules: {
          username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
          password: [{required: true, message: '请输入密码', trigger: 'blur'}],
          code: [{required: true, message: '请输入验证码', trigger: 'blur'}]
        }
      }
    },
    methods: {
      submitLogin() {
        this.$refs.loginForm.validate(valid => {
          // valid : 校验的成于否
          if (valid) {
            this.loading = true;
            this.postRequest('/login', this.loginForm)
                .then(res => {
                  if (res) {
                    // 有值即代表登录成功
                    // 得到后端传来，代表自己信息的令牌，并且保存下来
                    this.loading = false;
                    const tokenStr = `${res.obj.tokenHead}${res.obj.token}`;
                    localStorage.setItem("tokenStr", tokenStr);
                    this.$router.replace('/home')
                  }else{
                    // 登录不成功 加载动画取消
                    this.loading = false;
                  }
                })
          } else {
            this.$message.error('请输入所有字段！');
            return;
          }
        })
      },
      loginCaptcha() {
        this.captchaUrl = `/captcha?time=${Date.now()}`;
      }
    }
  }
</script>

<style lang="less">
  .loginContainer {
    width: 350px;
    box-shadow: 0 0 25px #cac6c6;
    background-clip: padding-box;
    margin: 180px auto;
    border-radius: 15px;
    padding: 15px 35px 15px 35px;
    border: 1px solid #eaeaea;

    .loginTitle {
      margin: 0px auto 30px auto;
      text-align: center;
    }

    .loginRemeber {
      margin-bottom: 15px;
      text-align: left;
    }
  }

  .el-form-item__content {
    display: flex;
    align-items: center;
  }
</style>
